package com.web.util;

import com.web.util.aliyun.ReplaceImgURL;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by reeves on 2015/8/31.
 */
public class HtmlStringHandler {

    /**
     * 对html string做处理
     *
     * @param htmlString
     * @return
     */
    public static String formatHtml(String htmlString) throws IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
        /*解析html片段*/
        Document document = Jsoup.parseBodyFragment(htmlString);

        Element body = document.body();


        /*移除p标签的align、class、style属性*/
        //document.select("p").removeAttr("align");
        body.select("p").removeAttr("margin-top");
        body.select("p").removeAttr("margin-bottom");
        body.select("p").removeAttr("text-align");


        /*移除img标签的一些属性*/
        body.select("img").removeAttr("style");
        body.select("img").removeAttr("id");
        body.select("img").removeAttr("align");
        body.select("img").removeAttr("border");
        body.select("img").removeAttr("alt");
        body.select("img").attr("width", "auto");
        body.select("img").attr("height", "auto");

        /*转换上传文章的图片路径*/
        Elements imgElements = document.select("img");

        for (Element element : imgElements) {
            String src = element.attr("src");
            String newsrc = ReplaceImgURL.replaceImgURL(src);
            if(newsrc.contains("@")){
                newsrc = newsrc.split("@")[0];
                element.attr("src",newsrc);
            }else{
                element.attr("src", newsrc + "@50q");
            }

            /*下面为了实现lightbox效果，效果不佳弃用*/
//            Element a = document.createElement("a");
//            a.attr("class", "boxer");
//            a.attr("href", newsrc);
//            Element img = document.createElement("img");
//            img.attr("src",element.attr("src"));
//            img.attr("width", "auto");
//            img.attr("height", "auto");
//
//            a.appendChild(img);
//
//            element.replaceWith(a);

        }

        /**
         * 加入js懒加载
         */
        /*Elements imgs = body.getElementsByTag("img");
        int n = imgs.size();
        for (Element img : imgs) {
            String src = img.attr("src");
            img.attr("data-original", src);
            img.attr("src", ReplaceImgURL.replaceImgURL(src));
        }*/

        /*移除空格,包括全角、半角空格*/
        String bodyStrWithBlank = body.html();
        //bodyStrWithBlank = bodyStrWithBlank.replaceAll(" ","");
        bodyStrWithBlank = bodyStrWithBlank.replaceAll("　", "");
        bodyStrWithBlank = bodyStrWithBlank.replaceAll("&nbsp;", " ");

        /*移除没有内容的段落和空行*/
        bodyStrWithBlank = bodyStrWithBlank.replaceAll("<p></p>", "");
        bodyStrWithBlank = bodyStrWithBlank.replaceAll("<p><br></p>", "");

        return bodyStrWithBlank;
    }

    /**
     * 回复文章内容
     */
    public static String restoreHtml(String htmlString) {
        Document document = Jsoup.parseBodyFragment(htmlString);
        Element body = document.body();

        Elements imgs = body.getElementsByTag("img");

        for (Element img : imgs) {
            String src = img.attr("src").split("@")[0];
            img.attr("src", src);
//            Element parent = img.parent();
//            parent.replaceWith(img);
        }

        String bodyStrWithBlank = body.html();

        return bodyStrWithBlank;
    }

}
